<?php


class RequestController extends Zend_Controller_Action
{
	
	
	public function init()
	{
		/* Initialize action controller here */
		
	}
	
	public function indexAction() 
	{

		// enable authentication
		$autentication = Zend_Registry::get('authetication');
		if(!$autentication)
		{
			$this->_helper->redirector('login', 'index');
		}
		
		
		$project = $this->loadprojectdata() ; 
		
		$project->name  ;
		$this->view->folder = $project->directory  ; 
		$this->view->project_name = $project->name  ; 
		
		
		
		$commits  = new Application_Model_DbTable_Commits() ;
		$select = $commits->select()
			->where('submited is null and author = \''.$autentication->username.'\' ');
		 				    
		$rows = $commits->fetchAll($select);
		$this->view->commits = $rows  ;
	}
	

	public function submitAction()
	{
	
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		
		$commits  = new Application_Model_DbTable_Commits() ;
		$project = $this->loadprojectdata() ;
	//	print_r($_REQUEST) ;  
		
		
		foreach ($_REQUEST['commits'] as $files)
		{
			// update revisions are submited 
			$data = array(
			   'submited' => '1'
			);
			$where['revision = ?'] = $files;
			$commits->update($data, $where);
			
			$revisions.= $files."|" ;
		} 
		
		
		$request  = new Application_Model_DbTable_Requests();
		$newRow = $request->createRow();
		
		$newRow->revisions= $revisions  ;
		$newRow->author=  $autentication->username ;
		$newRow->date= date("d/m/Y H:i:s");
		$newRow->message = utf8_encode($_REQUEST['message']) ;
		$newRow->subject = utf8_encode($_REQUEST['subject']) ;
		
		
		$newRow->save(); 
		
		
		
	
	}
	
	public function getcommitsAction()
	{ 
		
		$this->_helper->layout()->disableLayout();
		$autentication = Zend_Registry::get('authetication');
		$project = $this->loadprojectdata() ;
		
		$commits = new Application_Model_DbTable_Commits() ;
		$select = $commits->select()
		->where('submited is null and author = \''.$autentication->username.'\' and project=\''.$project->id.'\'');
		$rows = $commits->fetchAll($select);
		
		$this->view->commits = $rows  ;
		
	}
	
	
	public function pendentAction()
	{
	
		//$this->_helper->layout()->disableLayout();
	
		$requests  = new Application_Model_DbTable_Requests() ;
		$select = $requests->select()
		->where(' aprover is null  and  reprover is null ')
		->order('date asc') ;
			
		$rows = $requests->fetchAll($select);
		
		
		
		$project = $this->loadprojectdata() ;
		
		$project->name  ;
		
		$this->view->folder = $project->directory  ;
		$this->view->project_name = $project->name  ;
		$this->view->requests = $rows  ;
		
	
	}
	
	public function closedAction()
	{
	
		//$this->_helper->layout()->disableLayout();
	
		$requests  = new Application_Model_DbTable_Requests() ;
		$select = $requests->select()
		->where(' aprover is not null ')
		->order('id desc') ;
			
		$rows = $requests->fetchAll($select);
		
		
		$a = 0  ; 
		foreach ($rows as $row)
		{
			
			$commands  = new Application_Model_DbTable_Commands();
			$select_commands = $commands->select()
								->where(' request =  '.$row->id.' ') ; 
			$row_command = $commands->fetchAll($select_commands);
			
				
			
			$array_request[$a]['id'] = $row->id ;
			$array_request[$a]['revisions'] = $row->revisions ;
			$array_request[$a]['author'] = $row->author ;
			$array_request[$a]['aprover'] = $row->aprover ;
			$array_request[$a]['aprrover_text'] = $row->aprrover_text ;
			$array_request[$a]['date'] = $row->date ;
			$array_request[$a]['subject'] = $row->subject ;
			$array_request[$a]['message'] = $row->message ;
			$array_request[$a]['commands'] = $row_command ;
			//$array_request[$a]['backend_name'] = $row_command->cod_backend ;
				
			
			
			$a ++  ; 
			
		}
		
		
		$project = $this->loadprojectdata() ;
	
		$project->name  ;
	
		$this->view->folder = $project->directory  ;
		$this->view->project_name = $project->name  ;
		$this->view->requests = $rows  ;
		$this->view->requests_c = $array_request ;
		
		
		
	
	}
	
	public function reprovedAction()
	{
	
		//$this->_helper->layout()->disableLayout();
	
		$requests  = new Application_Model_DbTable_Requests() ;
		$select = $requests->select()
		->where(' reprover is not null ')
		->order('id desc') ;
			
		$rows = $requests->fetchAll($select);
	
	
		$a = 0  ;
		foreach ($rows as $row)
		{
				
			$commands  = new Application_Model_DbTable_Commands();
			$select_commands = $commands->select()
			->where(' request =  '.$row->id.' ') ;
			$row_command = $commands->fetchAll($select_commands);
				
			$array_request[$a]['id'] = $row->id ;
			$array_request[$a]['revisions'] = $row->revisions ;
			$array_request[$a]['author'] = $row->author ;
			$array_request[$a]['aprover'] = $row->aprover ;
			$array_request[$a]['aprrover_text'] = $row->aprrover_text ;
			$array_request[$a]['date'] = $row->date ;
			$array_request[$a]['subject'] = $row->subject ;
			$array_request[$a]['message'] = $row->message ;
			$array_request[$a]['commands'] = $row_command ;
				
			$a ++  ;
				
		}
	
	
		$project = $this->loadprojectdata() ;
	
		$project->name  ;
	
		$this->view->folder = $project->directory  ;
		$this->view->project_name = $project->name  ;
		$this->view->requests = $rows  ;
		$this->view->requests_c = $array_request ;
	
	
	
	
	}
	
	
	/* 
	 * when user aprove the request , this piece of code is executed  
	 */
	
	public function submitaproveAction()
	{
		
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		
		
		
		$project = $this->loadprojectdata() ; 
		
		//print_r($_REQUEST) ; 
		
		
		
		// aprove request 
		$requests  = new Application_Model_DbTable_Requests() ;
		
		$data = array(
				   'aprover' => $_REQUEST['approver'] ,
				   'aprrover_text' => $_REQUEST['message'] ,
				   'date_approved' => date("Y-m-d H:i:s")
		);
		
		
		$where['id = ?'] = $_REQUEST['request']  ;
		
		$requests->update($data, $where);
		
		//get revisions aprroved 
		$select = $requests->select()
		->where(" id = '".$_REQUEST['request']."' ");
		$rows = $requests->fetchAll($select);
		
		foreach ($rows as $row)
		{
			$revisions =  $row->revisions  ;
			//$rev = explode("|",$row->revisions) ;
		}
		
		
		$commits = new Application_Model_DbTable_Commits() ;
		$select = $commits->select()
		->where(' revision in ('.str_replace("|",",",$revisions).')');
		$rows_commit = $commits->fetchAll($select);
		
		$j = 0 ; 
		
		
		//get backends enabled aprroved
		$backends= new Application_Model_DbTable_Backends() ;
		
		$select_backends = $backends->select()
		->where(" enabled = 'Y' ");
		$rows_backends = $backends->fetchAll($select_backends);
		
		// navigate into backends 
		foreach ($rows_backends as $row_backend)
		{
			
			// generate de list of files that be changed  
			foreach ($rows_commit as $row)
			{
					
				$files = explode(" ",$row->files) ;
				foreach ($files as $row_file )
				{
					if($row_file != "")
					{
						$array_file[$j] = $row_file  ;
						$j ++ ;
					}
				}
					
			}
			
			// very important to reset counter 
			$j = 0 ;
			
			// commands copy
				
			for($i = 0 ; sizeof($array_file) > $i ; $i ++)
			{
			
			$command  = new Application_Model_DbTable_Commands();
			$newRow = $command->createRow();
			
			$newRow->cod_backend = $row_backend->id ;
			$cmd = "/usr/bin/svn update -r ".$row->revision."  ".$array_file[$i]."  " ;
			$newRow->command =  $cmd ;
			$newRow->date_scheduled = date("d/m/Y H:i:s");
			$newRow->request =  $_REQUEST['request']   ;
				
			
			$newRow->save();
			
			}
			
		} 
		
		
		
	}
	
	
	public function aproveAction()
	{
	
	
		// requests 
		$requests  = new Application_Model_DbTable_Requests() ;
		$data = $requests->find($_REQUEST['id']) ;
		$rows_revision = $data->current();
		$array_revisions = explode("|",$rows_revision->revisions) ; 
		$project = $this->loadprojectdata() ;
		
		// generate loop of revisions 
		if(sizeof($array_revisions) > 2 ) 
		{ 
			$i = 0 ; 
			
			foreach ($array_revisions as $row)
			{
	
				if($row != "")
				{ 
					
					if($i == 0 )
					{ 
						$revisions .= $row ;
						
					}
					else
					{ 
						$revisions .= ",".$row ;
					}
					
					$i ++ ; 
				} 
			} 
		}
		else
		{ 
			$revisions = $array_revisions[0] ; 
			
		}
		
		
		//echo $revisions ; 
		
		$commits = new Application_Model_DbTable_Commits() ;
		$select = $commits->select()
		->where(' revision in ('.$revisions.') and  project = '.$project->id.' ');
		
		//exit(); 
		
		$rows_commit = $commits->fetchAll($select); 
		
	//	print_r($rows_commit) ; 
		
		//get return of commands 
		/*$returns = new Application_Model_DbTable_Commands() ;
		$select = $commits->select()
		->where(' revision in ('.$revisions.') and  project = '.$project->id.' ');
		$rows_commit = $commits->fetchAll($select);*/ 
		
		
		
		
		/*
		// commits 
		$commits = new Application_Model_DbTable_Commits() ;
		$select = $commits->select()
		->where('submited is null and author = \'jorgelustosa@gmail.com\' ');
		$rows = $commits->fetchAll($select);
		*/ 
		
	 
		
		$autentication = Zend_Registry::get('authetication');
		
		
		$project->name  ;
		$this->view->folder = $project->directory  ;
		$this->view->project_name = $project->name  ;
		$this->view->revision = $rows_revision  ;
		$this->view->commits = $rows_commit  ;
		$this->view->autentication= $autentication  ;
		
		
		
	
	
	}
	
	
	public function pendentcountAction()
	{
		
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		
		
		$requests  = new Application_Model_DbTable_Requests() ;
		$select = $requests->select()
		->where(' aprover is null and reprover is null ');
		$rows = $requests->fetchAll($select);
		
		echo sizeof($rows) ; 
	} 
	
	
	
	private function loadprojectdata()
	{ 
		$data = new Application_Model_DbTable_Projects() ;
		$project = $data->find($_SESSION['Zend_Auth']['project']) ;
		$row = $project->current();
		return $row  ;
	}
	
	
	public function getcommands()
	{ 
		
		
		/*$commands  = new Application_Model_DbTable_Commands();
		$commands->find($_SESSION['Zend_Auth']['project']) ;*/ 
		
		
		return "jorge" ; 
		
	}
	
	
	public function reproveAction()
	{
	
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
	
	
	
		$project = $this->loadprojectdata() ;
	
		//print_r($_REQUEST) ;
	
	
	
		// aprove request
		$requests  = new Application_Model_DbTable_Requests() ;
	
		$data = array(
					   'reprover' => $_REQUEST['approver'] ,
					   'aprrover_text' => $_REQUEST['message'] ,
					   'date_reproved' => date("Y-m-d H:i:s")
		);
	
	
		$where['id = ?'] = $_REQUEST['request']  ;
	
		$requests->update($data, $where);
	
	} 	

	
	
	
	
	
	
	
} 
?>
